package com.atguigu.gmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.WebFilter;

/**
 * 
 * date:2022/8/27 11:28
 * 描述：
 **/
@Configuration
public class CorsConfig {
    @Bean
    public WebFilter webFilter(){
        //  在此定义规则：
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");    //  设置请求头
        corsConfiguration.addAllowedMethod("*");    //  设置请求方法
        corsConfiguration.addAllowedOrigin("*");    //  设置请求域名
        corsConfiguration.setAllowCredentials(true);//  允许携带cookie

        //  创建对象 编译时，看左边 == 运行时，看右边！
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        corsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(corsConfigurationSource);
    }
}